package com.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.R;
import com.common.ResultCode;
import com.common.SysSession;
import com.common.Util.StringUtils;
import com.user.annotation.AccessLimit;
import com.user.controller.vo.loginVo;
import com.user.controller.vo.userVo;
import com.user.entity.SysUser;
import com.user.service.IUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @Description:
* @Param:
* @return:
* @Author: 范铁凝
* @Date: 2024/6/15
*/
@RestController
@RequestMapping("/user")
public class userController {

    @Autowired
    private IUser Iuser;


    /*
    * @author 范铁凝
    * @date 2024/6/18
    * 登录接口
    */
    @PostMapping("/login")
    public R<Object> login(@Validated @RequestBody loginVo loginUser){

        SysUser user = Iuser.login(loginUser);
        if(ObjectUtil.isNull(user)){
            return R.fail(ResultCode.LOGIN_USERNAME_NOT_FOUND);
        }
        if(user.getIsDelete() != 0){
            return R.fail(ResultCode.LOGIN_USER_DISABLED);
        }
        StpUtil.login(user.getId());
        setSession(user);
        return R.data(Dict.create().set("token", StpUtil.getTokenValue()));

    }
    /*
    * @author 范铁凝
    * @date 2024/6/17
    * 发送验证码接口
    */
    @AccessLimit(count = 1, time = 60)
    @PostMapping("/code")
    public R<Boolean> sendCode(String phone){
        System.out.println(phone);
         if(!StringUtils.isValidPhone(phone)){
             return R.fail("请输入正确的手机号",false);
         }
        Iuser.sendCode(phone);
        return R.data(true);
    }
    /*
    * @author 范铁凝
    * @date 2024/6/17
    * register注册接口
    */
    @PostMapping("/register")
    public R<Object> register(@Validated @RequestBody userVo userVo){

        if(!Iuser.register(userVo)){
            return R.fail("注册失败",false);
        }
        SysUser user = Iuser.getOne(new QueryWrapper<SysUser>().eq("phone", userVo.getPhone()));
        StpUtil.login(user.getId());
        setSession(user);
        return R.data(Dict.create()
                        .set("token",StpUtil.getTokenValue())
                        .set("expires",StpUtil.getTokenTimeout())
                ,"注册成功");
    }

    /*
    * @author 范铁凝
    * @date 2024/6/18
    * 设置分布式会话信息
    */
    private void setSession(SysUser user){
        List<String> roleList = StpUtil.getRoleList();
        List<String> permissionList = StpUtil.getPermissionList();
        SysSession session = SysSession.builder().uid(user.getId())
                .username(user.getUsername())
                .phone(user.getPhone()).roleList(roleList)
                .permissionList(permissionList).build();
        StpUtil.getSession().set("LOGIN_USER", session);

    }
    /*
    * @author 范铁凝
    * @date 2024/6/19
    * 获取用户数据,用于代理人
    */
    @SaCheckLogin
    @GetMapping("/get/userInfo")
    public R<Object> get(){
        //获取所有用户信息
        List<Map<Long, String>> collect = Iuser.list()
                .stream().filter(user -> user.getIsDelete() == 0 || user.getId() != StpUtil.getLoginIdAsInt())
                .map(u -> Collections.singletonMap(u.getId(), u.getUsername())).collect(Collectors.toList());
        return R.data(collect);
    }
    /*
    * @author 刘童
    * @date 2024/6/21
    * 提交
    */
    @SaCheckPermission("role:ESP")
    @GetMapping("/improve")
//    提高用户权限
    public R<Object> getProductInfo(@RequestParam("user_id") long user_id, @RequestParam("role_id") long role_id) {


        return Iuser.improvePermission(user_id, role_id) ? R.data(true) : R.fail("修改权限失败");
    }


}
